import os
import django
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "baoxian.settings")
django.setup()
from langchain.tools import tool
from insurance.models import InsuranceInfo, ClaimOrder
from pydantic import BaseModel, Field
from langchain.tools import StructuredTool

class CreateClaimOrderSchema(BaseModel):
    id_number: str = Field(..., description="身份证号，18位")
    claim_amount: str = Field(..., description="理赔金额")

def create_claim_order_func(id_number: str, claim_amount: str) -> str:
    try:
        insurance = InsuranceInfo.objects.get(id_number=id_number)
    except InsuranceInfo.DoesNotExist:
        return "未找到用户保险信息，无法创建理赔订单"
    if float(claim_amount) > float(insurance.amount):
        return f"理赔金额 {claim_amount} 超过保额 {insurance.amount}，不能理赔"
    ClaimOrder.objects.create(
        insurance=insurance,
        claim_amount=claim_amount,
        status="待支付"
    )
    return f"理赔订单已创建，用户：{insurance.name}，理赔金额：{claim_amount}"

create_claim_order = StructuredTool.from_function(
    func=create_claim_order_func,
    name="create_claim_order",
    description="判断理赔金额是否超过保额，合法则创建理赔订单。",
    args_schema=CreateClaimOrderSchema,
)
